package global;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;

import model.UserModel;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.FileEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class Global {

	public static DefaultHttpClient client = new DefaultHttpClient();
	public static UserModel user = new UserModel();
	public static String BaseURI = "http://192.168.1.103:11111/";

	public static String POST(String Uri, List<NameValuePair> params) {

		HttpPost httpRequest = new HttpPost(BaseURI + Uri);
		try {
			// 发出HTTP request
			httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
			// 取得HTTP response
			HttpResponse httpResponse = client.execute(httpRequest);

			switch (httpResponse.getStatusLine().getStatusCode()) {
			// 若状态码为200 ok
			case 200:
				// 取出回应字串
				String strResult = EntityUtils.toString(httpResponse
						.getEntity());
				return strResult;
			case 401:
				return "身份验证失败，请重新登录";
			case 500:
				return "服务器发生错误，请与管理员联系";
			default:
				break;
			}
		} catch (ClientProtocolException e) {
			return "连接失败，请稍后重试";
		} catch (UnsupportedEncodingException e) {
			return e.getMessage();
		} catch (IOException e) {
			return e.getMessage();
		}
		return "连接失败，请稍后重试";
	}

	public static String Get(String Uri) {

		HttpGet httpRequest = new HttpGet(BaseURI + Uri);
		try {
			// 取得HTTP response
			HttpResponse httpResponse = client.execute(httpRequest);
			
			switch (httpResponse.getStatusLine().getStatusCode()) {
			// 若状态码为200 ok
			case 200:
				// 取出回应字串
				String strResult = EntityUtils.toString(httpResponse
						.getEntity());
				return strResult;
			case 401:
				return "身份验证失败，请重新登录";
			case 500:
				return "服务器发生错误，请与管理员联系";
			default:
				break;
			}
		} catch (ClientProtocolException e) {
			return "连接失败，请稍后重试";
		} catch (UnsupportedEncodingException e) {
			return e.getMessage();
		} catch (IOException e) {
			return e.getMessage();
		}
		return "连接失败，请稍后重试";
	}

	public static String UploadPost(String Uri, String filePath) {

		HttpPost httpRequest = new HttpPost(BaseURI + Uri);
		try {
			File fb = new File(filePath);
			// 发出HTTP request
			FileEntity fe = new FileEntity(fb, "binary/octet-stream");
			httpRequest.setEntity(fe);
			// 取得HTTP response
			HttpResponse httpResponse = client.execute(httpRequest);

			switch (httpResponse.getStatusLine().getStatusCode()) {
			// 若状态码为200 ok
			case 200:
				// 取出回应字串
				String strResult = EntityUtils.toString(httpResponse
						.getEntity());
				return strResult;
			case 401:
				return "身份验证失败，请重新登录";
			case 500:
				return "服务器发生错误，请与管理员联系";
			default:
				break;
			}
		} catch (ClientProtocolException e) {
			return "连接失败，请稍后重试";
		} catch (UnsupportedEncodingException e) {
			return e.getMessage();
		} catch (IOException e) {
			return e.getMessage();
		} catch (Exception e) {
			return null;
		}
		return "连接失败，请稍后重试";
	}

}
